<?php
namespace app\material\model;
use app\BaseModel;
use app\shop\model\AdminCompany;
use think\facade\Db;



class BrochureIm extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_material_brochure_im';

    protected $append = [

        'title'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-07-29 18:04
     * @功能说明:
     */
    public function getTitleAttr($value,$data){

        if(!empty($data['brochure_id'])&&isset($data['type'])){

            $model = $data['type']==1?new File():new Brochure();


            return $model->where(['id'=>$data['brochure_id']])->value('title');

        }


    }







    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;

    }




    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data=[],$type=1){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }





    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$map=[]){

        $data = $this->alias('a')
                ->join('longbing_card_user b','b.id = a.user_id')
                ->where($dis)
                ->where(function ($query) use ($map) {
                    $query->whereOr($map);
                })
                ->field('a.*,b.nickName,b.avatarUrl')
                ->group('a.id')
                ->order('a.id desc')
                ->select()
                ->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataPageList($dis,$map=[],$page=10){

        $data = $this->alias('a')
            ->join('longbing_card_user b','b.id = a.user_id')
            ->where($dis)
            ->where(function ($query) use ($map) {
                $query->whereOr($map);
            })
            ->field('a.*,b.nickName,b.avatarUrl')
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();

        if(!empty($data['data'])){


            foreach ($data['data'] as &$v){

                $v['time_long']  = !empty($v['time_long'])?lbGetDatesss($v['time_long']):'0秒';


            }

        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-08-11 11:12
     * @功能说明:添加录音
     */
    public function addAudio($room_key,$url=''){

        $dis = [

            'room_key' => $room_key,

            'leader'   => 1

        ];
        //查询房间
        $room = $this->dataInfo($dis);

        $folder_model = new Folder();

        $dis = [

            'user_id' => $room['user_id'],

            'title'   => '文件演示录音'
        ];

        $folder = $folder_model->where($dis)->where('status','>',-1)->find();
        //初始化录音文件夹
        if(empty($folder)){

            $insert = [

                'user_id' => $room['user_id'],

                'title'   => '文件演示录音',

                'uniacid' => $room['uniacid'],

                'pid'     => 0,

                'status'  => 1
            ];

            $folder_model->dataAdd($insert);

            $folder_id = $folder_model->getLastInsID();

        }else{

            $folder = $folder->toArray();

            $folder_id = $folder['id'];
        }
        //合并录音
        $im_video_model = new ImVideo();

        $video_list = $im_video_model->where(['room_key'=>$room_key])->column('video');

        if(!empty($video_list)){
            //合并录音
            $video_link = mergeFilev2($video_list,$url);

            $file_model = $room['type']==1 ?new File():new Brochure();

            $title = $file_model->where(['id'=>$room['brochure_id']])->value('title');

            $video_title = $title.'-'.date('Y-m-d',time());

            $insert = [

                'uniacid'  => $room['uniacid'],

                'title'    => $video_title,

                'folder_id'=> $folder_id,

                'link'     => $video_link,

                'user_id'  => $room['user_id'],

                'file_type'=> 6,

                'imgs'     => '',

                'room_key' => $room_key

            ];

            $file_model = new File();

            $file_model->dataAdd($insert);

        }

        return true;

    }



}